<?

JCAT_Package::ImportClass('JCAT_ViewGtk',JCAT::PACKAGE_MVCGTK) ;

class CreateUICtrlFromGladeDlg extends JCAT_GtkDialog implements JCAT_IViewForm
{
	/**
	 * 控件基本信息
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	void
	 */
	public function CreateUICtrlFromGladeDlg( $sSaveDirectory='', JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_GtkDialog('CreateUICtrlFromGladeDlg','ViewGtkDlg','CreateUICtrlFromGladeDlg',$aParent,$aUI) ;

		// 创建 并 初始化 UICtrl ---------------------------
		$sUICtrlDir = dirname(__FILE__).'/' ;
		$aUICtrlSaveDir = JCAT_Package::ImportInstance('UICtrlDirectory',$sUICtrlDir) ;
		$this->AddUICtrl($aUICtrlSaveDir,'UICtrlDirectory') ;

		$this->AddUICtrl(JCAT_Package::ImportInstance('WidgetId',$sUICtrlDir),'WidgetId') ;
		$this->AddUICtrl(JCAT_Package::ImportInstance('AutoAppend',$sUICtrlDir),'AutoAppend') ;

		// 初始化控件值
		$aUICtrlSaveDir->SetDefaultText($sSaveDirectory) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function AddWidget($sWidgetId,$sWidgetClass,$sCoderControllerName)
	{
		$aUICtrl = $this->GetUICtrl('WidgetId') ;
		$aUICtrl->AddOption("{$sWidgetId} ({$sWidgetClass})","{$sCoderControllerName}:{$sWidgetId}") ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBrowseDirectory()
	{
		JCAT_Package::ImportClass('JCAT_GtkFSDialog',JCAT::PACKAGE_MVCGTK) ;
		
		$sDirPath = JCAT_GtkFSDialog::Choose(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER,null,"Glade	*.glade") ;
		if($sDirPath===NULL)
		{
			return ;
		}
		
		$aUICtrl = $this->GetUICtrl('UICtrlDirectory') ;
		$aUICtrl->SetDataByExchange($sDirPath) ;
	}


	public function OnOk()
	{
		$sWidget = $this->GetUICtrlExchangeData('WidgetId') ;
		if(!$sWidget)
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择窗体')) ;
			return ;
		}

		// 控制器 和  窗体名称
		list($sController,$sWidgetName) = explode(':',$sWidget) ;

		// 目录
		$arrArg['widget'] = array() ;
		$aUICtrlDirectory = $this->GetUICtrl('UICtrlDirectory') ;
		$sDirectory = $aUICtrlDirectory->GetExchangeData() ;
		if($sDirectory)
		{
			$arrArg['path'] = $sDirectory ;
		}
		
		// 调用对应的 Coder 类IToolboxUIGtkCtrlCoder
		JCAT_ASSERT::ASSERT_IMPLEMENTS($sController,'JCAT_IController');
		JCAT_ASSERT::ASSERT_IMPLEMENTS($sController,'IToolboxUIGtkCtrlCoder');
		$aController = new $sController() ;
		$aController->SetWidgetId($sWidgetName) ;
		$sPath = $aController->MainRun(&$arrArg) ;
		
		if( !is_file($sPath) )
		{
			return ;
		}

		// 载入 控件
		if( $this->GetUICtrlExchangeData('AutoAppend') )
		{
			$aController = $this->GetParentController() ;
			$aTopController = $aController->GetTopController() ;
			$aUICtrlChildrenController = $aTopController->GetController('UICtrlContainer') ;
			JCAT_ASSERT::ASSERT_INSTANCE($aUICtrlChildrenController,'UICtrlContainer') ;
			
			$aUICtrlChildrenController->LoadUICtrl($sPath) ;
		}
	}
}

?>